<?php

namespace app\store\controller\integral;

use app\store\controller\BaseController;
use app\store\model\IntegralGoods;
use app\store\service\IntegralIntervalService;
use app\store\service\IntegralGoodsService;
use app\store\service\GoodsService;
use think\facade\View;
use think\exception\ValidateException;

class GoodsController extends BaseController
{
    public function index()
    {   
        $param = $this->request->get();

        $integralIntervalService = new IntegralIntervalService();
        $options = $integralIntervalService->getIntegralIntervalOptions();

        $GoodsGoodsService = new IntegralGoodsService();
        $list = $GoodsGoodsService->getIntegralGoodsList($param);


        return View::fetch('',['options' => $options,'list' => $list]);
    }
    
    public function create()
    {
        $id = $this->request->param('id/d',0);

        $integralIntervalService = new IntegralIntervalService();
        $options = $integralIntervalService->getIntegralIntervalOptions();


        if (!$id){
            return View::fetch('',['options' => $options]);
        }

        $goodsService = new GoodsService();
        $row = $goodsService->findGoods($id);



        return View::fetch('',['options' => $options,'row' => $row]);
    }


    public function edit()
    {   
        $id = $this->request->get('id/d');

        $integralIntervalService = new IntegralIntervalService();
        $options = $integralIntervalService->getIntegralIntervalOptions();

        $integralGoodsService = new IntegralGoodsService();
        $row = $integralGoodsService->findIntegralGoods($id);

        if(!$this->request->isPjax()){
            View::engine()->layout(true);
        }

        return View::fetch('',['options' => $options, 'row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();

        try {
            validate('IntegralGoodsValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goodsGoodsService = new IntegralGoodsService();
        $goods_id = $goodsGoodsService->saveIntegralGoods($param);
        
        return  $goods_id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);

    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('IntegralGoodsValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        IntegralGoods::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function update()
    {
        $param = $this->request->post();

        try {
            validate('IntegralGoodsValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $GoodsGoodsService = new IntegralGoodsService();
        $id =  $GoodsGoodsService->updateIntegralGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);
    }

    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = IntegralGoods::destroy($id);

        return $result ?  json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }
}